package com.ruoyi.collection.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.base.yaml.BusinessConfig;
import com.ruoyi.bizsys.domain.ExtIdentityResult;
import com.ruoyi.collection.domain.ApiParameter;
import com.ruoyi.collection.domain.CollectionRepaymentResp;
import com.ruoyi.collection.domain.QueryIdentityReq;
import com.ruoyi.collection.mapper.CollectionExtIdentityResultMapper;
import com.ruoyi.collection.process.api.CollectionProcess;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;

/**
 * 催收接口类
 * @Author: Victor
 */
@Slf4j
@Api("催收模块接口")
@RestController
public class CollectionController {
	
	@Autowired
	private Map<String, CollectionProcess> collectionProcessList;

	@Resource
	private CollectionExtIdentityResultMapper collectionExtIdentityResultMapper;

	@Autowired
	BusinessConfig businessConfig;
	private static RestTemplate restTemplate;
	static {
		SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
		factory.setConnectTimeout(180000);
		factory.setReadTimeout(180000);
		restTemplate = new RestTemplate(factory);
	}


	@RequestMapping(value = { "/collection/call" }, method = { RequestMethod.POST }, produces="application/json;charset=UTF-8")
    public String call(@RequestBody String jsonReq) {
		log.info("jsonReq:::"+jsonReq);
		ApiParameter resp = new ApiParameter();
		try {
			resp.getHead().setResultCode("0000");
			ApiParameter req = JSON.parseObject(jsonReq, ApiParameter.class);
			CollectionProcess collectionProcess = collectionProcessList.get(req.getHead().getMessageId());
			resp.setBody(collectionProcess.process(req));
		} catch (Exception e) {
			log.error("异常",e);
			resp.getHead().setResultCode("9999");
			resp.getHead().setResultMessage(e.getMessage());
		}
		JSONObject respJson = (JSONObject) JSONObject.toJSON(resp);
		return respJson.toString();
    }

	@RequestMapping(value = { "/collection/walletpayForDefer" }, method = { RequestMethod.POST }, produces="application/json;charset=UTF-8")
	public String walletpayForDefer(@RequestBody String jsonReq) {
		log.info("jsonReq:::"+jsonReq);
		ApiParameter resp = new ApiParameter();
		try {
			//	/forcollection/collection/walletpayForDefer?loanSerialNo=&deferredId=
			resp.getHead().setResultCode("0000");
			ApiParameter req = JSON.parseObject(jsonReq, ApiParameter.class);
			JSONObject param = JSON.parseObject(req.getBody().toString());
			resp.setBody(walletBuyDefer(param));
		} catch (Exception e) {
			log.error("异常",e);
			resp.getHead().setResultCode("9999");
			resp.getHead().setResultMessage(e.getMessage());
		}
		JSONObject respJson = (JSONObject) JSONObject.toJSON(resp);
		return respJson.toString();
	}

	private Object walletBuyDefer(JSONObject param) {
		log.info("wqg:调用业务系统进行查询-钱包购买延期--"+param.toJSONString());
		String loanSerialNo = param.getString("loanSerialNo");
		String deferredId = param.getString("deferredId");
		String url = businessConfig.getApiurl()+"/forcollection/collection/walletpayForDefer?loanSerialNo="+loanSerialNo+"&deferredId="+deferredId;
        log.info("wqg:调用业务系统进行查询-钱包购买延期-url--"+url);
		JSONObject res = restTemplate.getForObject(url,JSONObject.class,"");
        log.info("wqg:调用业务系统进行查询-钱包购买延期-结果--"+res.toJSONString());
		return res;
	}





    @PostMapping("/collection/listIdentity")
	public String listIdentity(@RequestBody String jsonReq){
		try {
			ApiParameter req = JSON.parseObject(jsonReq, ApiParameter.class);
			QueryIdentityReq reqBody = JSON.parseObject(req.getBody().toString(), QueryIdentityReq.class);
			if (StringUtils.isBlank(reqBody.getBvn()) && StringUtils.isBlank(reqBody.getCustNo())){
				return JSONObject.toJSONString(new ArrayList<>());
			}
			List<ExtIdentityResult> extIdentityResults = collectionExtIdentityResultMapper.listIdentity(reqBody.getBvn(),reqBody.getCustNo());
			return JSONObject.toJSONString(extIdentityResults);
		} catch (Exception e) {
			return JSONObject.toJSONString(Collections.emptyList());
		}

	}

}
